// var app = require('../app');
var SerialPort = require("serialport");
var serialport = require("../routes/serialport");
// var socket = require("../routes/socket")
var assert = require("assert");
var net  = require('net');
var db = require('../routes/db');
var parseMessage = require('../routes/parseMessage');
var util = require('../routes/util');

var shore_base_platform_send = "";
var shore_base_platform_receive = "";

var serial_port = "COM6";
var serial = new SerialPort(serial_port, {
    baudrate: 115200, // this is synced to what was set for the Arduino
    dataBits: 8, // this is the default for Arduino serial communication
    parity: 'none', // this is the default for Arduino serial
    stopBits: 1, // this is the default for Arduino serial communications
}); // this is the openImmediately flag [default is true]

serial.on('error', function(err) {
    console.log('Error: ', err.message);
})

serial.on('data', function (data) {
    // shore_base_platform_send += data.toString().replace(/[\r\n]/g,"");
    shore_base_platform_send += data.toString();
    console.log("串口收到的信息：");
    console.log(shore_base_platform_send);
});

var client= new net.Socket();
var host = '192.168.1.13';
var port = 1310;
client.connect(port,host,function(err){
    console.log("socket 开启");
});
client.on('data',function(data){
    console.log("socket接收到的数据");
    console.log(data.toString());
    shore_base_platform_receive = data.toString().replace(/[\r\n]/g,"");
});

describe('test.js', function() {
    describe("shore based platform send message to beidou platform", function () {
        // this.timeout(5000);
        // it("should combine A4Message if message sended by shore based platform is protocol corrected", function (done) {
        //     var receiver = "1244567";
        //     var content = "组装A4报文测试";
        //     var A4Msg = util.combineA4Msg(receiver, content).replace(/[\r\n]/g,"");
        //     assert.equal(A4Msg, "$CCTXA,1244567,1,2,A4e7bb84e8a3854134e68aa5e69687e6b58be8af95*57");
        //     console.log(A4Msg);
        //     console.log("组装A4报文函数测试成功");
        //     done();
        // });
        //
        // it("should reply message if message sended by shore based platform is protocol corrected", function (done) {
        //     var message = "$BDFKI,TXA,Y,Y,0,0000*13";
        //     serial.write(message);
        //     done();
        // });
        //
        // it("message will be sended by serial port if socket sends message",function (done) {
        //     var client= new net.Socket();
        //     var host = '192.168.1.13';
        //     var port = 1310;
        //     client.connect(port,host,function(err){
        //     });
        //
        //     var data = "$BDMSG,1244564,写库测试*";
        //     client.write(data);
        //
        //     setTimeout(function () {
        //         //assert.equal(shore_base_platform_send.replace(/[\r\n]/g,""), "$CCTXA,1244564,1,2,A4e58699e5ba93e6b58be8af95*00");
        //         done();
        //     }, 3000);
        //
        //     console.log('waiting for 4 seconds.');
        //     //done();
        // });
        //
        // it("message is written into mysql if socket send message", function (done) {
        //     console.log("message in db");
        //     db.sequelize.query('select * from DownMessage where receiver = "1244564" order by time desc').spread(function (results, metadata) {
        //         //console.log(results);
        //         var content = results[0].content;
        //         assert.deepEqual(content, "写库测试");
        //         console.log("插入数据库成功");
        //         done();
        //     });
        // });
    });


    describe("beidou platform receive message from ship and send to shore based platform", function (done) {
        // this.timeout(5000);
        // it("should parse message if message type is 0", function (done) {
        //     var message = "BDTXR,1,0458120,0,,类型为0*84";
        //     var content = parseMessage.parseUploadMessage(message);
        //     assert.equal(content, "$BDPOS,0458120,类型为0*");
        //     console.log("parseUploadMessage函数测试成功");
        //     done();
        // });
        // it("should parse message if message type is 1", function (done) {
        //     var message = "BDTXR,1,0458120,1,,123*7e";
        //     var content = parseMessage.parseUploadMessage(message);
        //     assert.equal(content, "$BDPOS,0458120,123*");
        //     console.log("parseUploadMessage函数测试成功");
        //     done();
        // });
        // it("should parse message if message uploaded is protocol corrected", function (done) {
        //     var message = "BDTXR,1,0458120,2,,A43132332e3730353931382c452c33302e3436343932382c4e2c323031372d30332d30312031343a33303a32372c30*30";
        //     var content = parseMessage.parseUploadMessage(message);
        //     assert.equal(content, "$BDPOS,0458120,123.705918,E,30.464928,N,2017-03-01 14:30:27,0*");
        //     console.log("parseUploadMessage函数测试成功");
        //     done();
        // });
        it("should send upload message to shore based platform if message uploaded is protocol corrected", function (done) {
            var message = "$BDTXR,1,0458120,2,,A43132332e3730353931382c452c33302e3436343932382c4e2c323031372d30372d313120383a35383a32372c30*3f";
            serial.write(message);

            // setTimeout(function () {
            //    // assert.equal(shore_base_platform_receive, "$BDPOS,0458120,123.705918,E,30.464928,N,2017-07-11 8:58:27,0*");
            //     done();
            // }, 3000);
            console.log('waiting for 3 seconds.');
        })
        // it("should write upload message into mysql if message uploaded is protocol corrected", function (done) {
        //     db.sequelize.query('select * from UploadLocationUniq where sender = "0458120"').spread(function (results, metadata) {
        //         console.log(results);
        //         var content = results[0].lon;
        //         assert.deepEqual(content, "123.706");
        //         console.log("插入数据库成功");
        //         done();
        //     });
        // })
    });
});